package com.turing.data.scripts.zjtq.filter

import com.turing.common.util.FileUtils
import com.turing.common.util.ObjectUtils
import com.turing.common.util.StringUtils
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import com.turing.data.engine.utils.RegexExtract

/**
 * 场景名称：追加提取单
 * 要素名称：日期
 **/

class Date_30231_34_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        def JSONTextMap = dependParams.JSONTextMap
        def textPathMap = dependParams.textPathMap
        List<TaskElementVO.DrawResultVO > trueValueList = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        try {
            if (resultVOs!=null && resultVOs.size()>0) {

                def resultVO = resultVOs.get(0)
                def text = resultVO.text
                def childTaskSn = resultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == "" || StringUtils.isEmpty(text)){
                    trueValueList.add(resultVOs.get(0))
                    return trueValueList
                }
                def start = resultVO.start
                def end = resultVO.end

                def rawPath = textPathMap.get(resultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def fileTextStr = ""
                fileTextStr = JSONTextMap.get(childTaskSn)
                if(!text.endsWith("日") && text.substring(text.length()-1,text.length()) =~ /\d/ \
                    && end +1 <= rawText.length() && rawText.substring(end,end+1) =~ /\d/ ){
                    TaskElementVO.DrawResultVO ObjectNew = (TaskElementVO.DrawResultVO) ObjectUtils.deepClone(resultVO)
                    ObjectNew = TextUtils.correctPositionInfoByOffset(ObjectNew, 0, 1, fileTextStr, rawText)
                    ObjectNew.text = rawText.substring(start, end + 1 )
                    ObjectNew.businessValue = rawText.substring(start, end + 1 )
                    trueValueList.add(ObjectNew)
                    return trueValueList
                }

                if(resultVOs.get(0).text.contains("年月日")){
                    List< TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
                    def REGEX = /\d{4}年\d{1,2}月\d{1,2}日/
                    dependParams.JSONTextMap.forEach(
                        { childTaskSn2, jsonText ->
                            RegexExtract.doRuleExtract(REGEX,childTaskSn2, dependParams, jsonText, trueValueSet)
                        }
                    )
                    return trueValueSet
                }

                trueValueList.add(resultVOs.get(0))
                return trueValueList
            }

        }catch(Exception e){
            e.printStackTrace()
        }

        trueValueList.add(resultVOs.get(0))
        return trueValueList
    }

}
